<template>
	<theme-box title="管理员查询">
		<form class="form-inline" @submit.prevent v-el:searchform>
			<div class="form-group">
				<label>操作员名称：</label>
				<input type="text" class="form-control" v-el:searchname>
			</div>
			<div class="form-group">
				<label>操作员状态：</label>
				<select class="form-control" type="text" v-el:searchstate>
					<option value="">所有状态</option>
					<option value="0">正常</option>
					<option value="1">锁定</option>
				</select>
			</div>
			<v-button theme="warning" class="pull-right" @click="reset">重置</v-button>
			<v-button theme="primary" class="pull-right" @click="search">查询</v-button>
		</form>
		<table class="table table-bordered table-striped" v-el:table>
		</table>
	</theme-box>
</template>

<style scoped>
	.form-inline .btn+.btn {
		margin-right: 10px;
	}
	
	.form-inline .form-group+.form-group {
		margin-left: 20px;
	}
</style>

<script>
	import {
		deepClone
	}
	from 'lib/clone.js';

	let datatable;
	export default {
		data: () => ({
			dataSet: {
				data: [],
				recordsTotal: 0,
				recordsFiltered: 0
			},
			columnsSet: []
		}),
    events: {
      'table-reload': function () {
      	datatable.api().ajax.reload();
			}
    }, 		
		methods: {
			  reset() {
					this.$els.searchform.reset();
					datatable.api().ajax.reload();
				},
				search() {
					datatable.api().ajax.reload();
				},
				setConfig() {
					//..加载配置
					let self = this;
						this.columnsSet = [
						{
								title: "操作员编号",
								data: "oprId",
								className: "first-td"
						},
						{
								title: "操作员名称",
								data: "oprName"
						},
						{
								title: "角色",
								data: "roleName"
						},					
						{
								title: "机构编号",
								data: "brhNo"
						},								
						{
							title: "操作员描述",
							data: "oprDsc",
						  width: "250px",							
							render: function (data) {
								return data||"";
							}
						},
						{
								title: "状态",
								data: "oprStat",
								render: function (data) {
									return (data == 0)?"正常":"锁定"
								}
						}						
					];
				},
				loadData(data = {}) {
					//..加载数据
					let self = this;
					return new Promise(function (resolve, reject) {
						self.$post({
							url: '/api/v1' + projectName_Url + '/sys/opr/query.html',
							data
						}).then( response => {	
							if (response) {
								let content = response.content;
								self.dataSet.data = content.oprList;
								self.dataSet.recordsTotal = content.total;
								self.dataSet.recordsFiltered = content.total;
								return resolve();
							}
						}, response => {
							  return reject();
						});

					})
				},
				getCollection() {
					let collection = {};
					let self = this;
					this.$post({
						url: "/api/v1" + projectName_Url + "/sys/role/query.html"
					}).then(function (response) {
						collection.role = response.info;
					});
					return collection;
				}
		},
		created() {
			this.setConfig();
		},
		ready() {
			let self = this;

			datatable = $(this.$els.table).dataTable({
				"columns": self.columnsSet,
				"paging": true,
				"pagingType": "full_numbers",
				"lengthChange": false,
				"searching": false,
				"ordering": false,
				"info": true,
				"autoWidth": false,
				"serverSide": true,
				"processing": true,
				"ajax": function (data, callback, settings) {

					let result = {};
					let _data = {};

					_data.aoData = JSON.stringify([{
						"name": "iDisplayStart",
						"value": 1 + data.start / data.length
									}, {
						"name": "iDisplayLength",
						"value": data.length
									}]);

					$(self.$els.searchform).find("input").each(function () {
						$(this).val($.trim($(this).val()));
					});
					
					_data.oprName = self.$els.searchname.value;
					_data.oprStat = self.$els.searchstate.value;

					self.loadData(_data).then(function () {
						result = self.dataSet;
						if (!result.data) {
							result.data = [];
						}
						callback(result);
					}, function () {
						result.data = [];
						callback(result);
					})

				},
				"fnDrawCallback": function( settings ) {
					let count = Math.ceil(settings._iRecordsTotal/settings._iDisplayLength);
					if(count<=1) {
						  $('.dataTables_paginate').hide();
					} else {
						  $('.dataTables_paginate').show();
					}
				}					

			});
		}
	}
</script>